#include "benchmark.h"
#include "genlib.h"
#include "strlib.h"
#include <stdio.h>

bool benchAddOperation(benchmarkT b) {
	return b->numCritOp++;
}

void benchmarkStart(char *name, benchmarkT b) {
	b->name = name;
	b->numCritOp = 0;
	b->end = 0;
	b->elapsed = 0;
	b->start = (double) clock() / CLOCKS_PER_SEC;
}

void benchmarkEnd(benchmarkT b) {
	b->end = (double) clock() / CLOCKS_PER_SEC;
	b->elapsed = (b->end - b->start);
}

void saveBenchToFile(algorithmT alg, int numRecords, int arrayType, int avgCritOp, double avgTime) {
	char *fileName;
	FILE *inFile;

	string prefix = Concat(IntegerToString(arrayType), "-");
	switch (alg) {
		case QUICK_SORT:		fileName = Concat(prefix, "quick_sort.txt");			break;
		case SELECTION_SORT:	fileName = Concat(prefix, "selection_sort.txt");	break;
		case INSERTION_SORT:	fileName = Concat(prefix, "insertion_sort.txt");	break;
		case BUCKET_SORT:		fileName = Concat(prefix, "bucket_sort.txt");		break;
		case MERGE_SORT:		fileName = Concat(prefix, "merge_sort.txt");			break;
		default:					printf("Couldn't save the file.\n");					return;
	}

	// Open/Create file for appending data to:
	inFile = fopen(fileName, "a+");

	// Row:
	if (inFile != NULL) {
		fprintf(inFile,"%d\t%d\t%f\n", numRecords, avgCritOp, avgTime);
		fclose(inFile);
	}
}

void printBenchmark() {
	printf("\n\n---\nAlgorithm: %s\nCritical operations hits: %d\nExecution time: %f sec\n",
		benchmark->name, benchmark->numCritOp,
		benchmark->elapsed, benchmark->end, benchmark->start);
}